package com.matthewtamlin.fortytwo.example;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

import com.matthewtamlin.fortytwo.library.answer_group.SelectionLimitedAnswerGroup;
import com.matthewtamlin.fortytwo.library.answer_view.AnswerView;

/**
 * QuestionAbility
 *
 * @since 2021-07-07
 */
public class AbstractQuestionAbility extends Ability {
    /**
     * Contains the question.
     */
    private Text questionContainer;

    /**
     * Contains the answers.
     */
    private SelectionLimitedAnswerGroup answerGroup;

    /**
     * Button for submitting and resetting the answers.
     */
    private Button actionButton;

    /**
     * Whether or not the answers are currently submitted.
     */
    private boolean currentlyMarked = false;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_question_ability);

        questionContainer = (Text) findComponentById(ResourceTable.Id_main_Activity_question_container);
        answerGroup = (SelectionLimitedAnswerGroup) findComponentById(ResourceTable.Id_main_activity_answer_group);
        actionButton = (Button) findComponentById(ResourceTable.Id_main_activity_action_button);

        actionButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                for (final AnswerView answerView : answerGroup.getAnswers()) {
                    answerView.setMarkedStatus(!currentlyMarked, true);
                }

                currentlyMarked = !currentlyMarked;

                if (currentlyMarked) {
                    actionButton.setText("UNSUBMIT");
                } else {
                    actionButton.setText("SUBMIT");
                }
            }
        });
    }

    public Text getQuestionContainer() {
        return questionContainer;
    }

    public SelectionLimitedAnswerGroup getAnswerGroup() {
        return answerGroup;
    }
}
